import React from 'react';
import { observer, inject } from 'mobx-react';
import { _,FieldUtil } from '../../util/pcPublic';
import * as API_REQ from '../../apis/form';
import WfInput from '../../public/wf-form-input/index';
import { getLabel } from '../../util/common';

//流程编号
@inject('wfGlobalStore','wfLayoutStore')
@observer
export default class WeaWfCode extends React.Component {
	constructor(props) {
		super(props);
    }
    doChangeEvent(value){
        const { fieldMark,wfLayoutStore } = this.props;
		wfLayoutStore.changeSingleFieldValue(fieldMark, {value: value});
    }
	render() {
        const { wfGlobalStore,wfLayoutStore } = this.props;
        const { commonParam } = wfGlobalStore;
        const { iscreate } = commonParam;
        const { codeInfo } = wfLayoutStore;
        const { codefields,hasHistoryCode } = codeInfo;
        const { fieldid,fieldMark,fieldValue,viewAttr,fieldAttr,symbol,fieldObj } = this.props;
        const isShowBtn = !hasHistoryCode && iscreate != "1" && !commonParam.belongTest;
        return (
            <WfInput
                viewAttr={viewAttr}
                fieldid={fieldid}
                fieldName={fieldMark}
                fieldMark={fieldMark}
                isDetail={false}
                detailtype={1}
                value={fieldValue}
                onChange={this.doChangeEvent.bind(this)}
            />
        )
    }
    getRelateParams(){
        const { wfGlobalStore,wfLayoutStore } = this.props;
        const { codeInfo } = wfLayoutStore;
        const { codefields } = codeInfo;
        const { baseParam,commonParam } = wfGlobalStore;
        const params = {...baseParam, ...{creater:commonParam.creater, creatertype:commonParam.creatertype}};
        codefields && codefields.map(field =>{
            const fieldid = `${field.split("_")[0]}`;
            params["field"+fieldid] = FieldUtil.getRelateFieldValue(fieldid, -1);
        });
        return params;
    }
	generateCode() {
        const { fieldMark,wfLayoutStore } = this.props;
        let params = this.getRelateParams();
        params.operation = "CreateCodeAgain";
		API_REQ.createWfCode(params).then(data =>{
            const codeValue = data.wfcode;
            wfLayoutStore.changeSingleFieldValue(fieldMark, {value: codeValue});
            this.syncTitleRequestMark(codeValue);
		});
	}
	chooseReservedCode() {
        // const { fieldMark } = this.props;
        // let params = this.getRelateParams();
        // const { workflowid,formid,isbill } = params;
		// API_REQ.loadWfCodeFieldValueInfo(params).then(data =>{
        //     let chooseParams = {...data, ...{isRoute:true, isFromForm:1, workflowId:workflowid}};
        //     let url = `/spa/workflow/static4engine/engine.html#/main/workflowengine/path/codeMaintenance/reserveCode?${buildUrlStr(chooseParams)}`;
        //     const dialog = WeaTools.createDialog({
        //         title: getLabel(22785,'选择预留号'),
        //         moduleName: "workflow",
        //         url: url,
        //         style: {width:800, height:600},
        //         callback: (con) =>{
        //             params.codeSeqReservedIdAndCode = con.id+"~~wfcodecon~~"+con.reservedCode;
        //             params.operation = "chooseReservedCode";
        //             API_REQ.createWfCode(params).then(data =>{
        //                 const codeValue = data.wfcode;
        //                 allStore.layoutStore.changeSingleFieldValue(fieldMark, {value: codeValue});
        //                 this.syncTitleRequestMark(codeValue);
        //             });
        //         },
        //         onCancel: () =>{
        //         }
        //     });
        //     dialog.show();
		// });
	}
	newReservedCode() {
		// const { fieldMark } = this.props;
        // let params = this.getRelateParams();
        // const { workflowid,formid,isbill } = params;
		// API_REQ.loadWfCodeFieldValueInfo(params).then(data =>{
        //     let createParams = {...data, ...{isRoute:true, isFromForm:1, workflowId:workflowid}};
        //     let url = `/spa/workflow/static4engine/engine.html#/main/workflowengine/path/codeMaintenance/addReserveCode?${buildUrlStr(createParams)}`;
        //     const dialog = WeaTools.createDialog({
        //         title: getLabel(22783,'新建预留号'),
        //         moduleName: "workflow",
        //         url: url,
        //         style: {width:600,height:600},
        //         callback: (datas) =>{
        //         },
        //         onCancel: () =>{
        //         }
        //     });
        //     dialog.show();
		// });
    }
    syncTitleRequestMark(mark) {
        // jQuery("#requestmarkSpan").text(mark);
    }
}